/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.only.fx;

import javafx.beans.Observable;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.effect.BlurType;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.Background;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
 *
 * @author Only
 */
public class OnlyFrame extends OnlyStage {

	// private final GaussianBlur gaussianBlur = new GaussianBlur();
	private final DropShadow dropShadow = new DropShadow();
	private final StackPane rootPane = new StackPane();

	private final StackPane backgroundColorPane = new StackPane();
	protected final StackPane backgroundImagePane = new StackPane();
	private final StackPane stackPane = new StackPane();
	private final StackPane centerPane = new StackPane();
	private final Rectangle clip = new Rectangle();
	private TitlePane titlePane;
	private final OnlyScene scene = new OnlyScene(getRootPane());
	private final Move move = new Move(this, scene);

	public OnlyFrame() {
		initFrame();
		initEvent();
	}

	private void initFrame() {
		this.setScene(scene);

		titlePane = move.getTitlePane();
		scene.setFill(Color.TRANSPARENT);

		dropShadow.setBlurType(BlurType.GAUSSIAN);

		rootPane.setBackground(Background.EMPTY);
		rootPane.setEffect(dropShadow);
		rootPane.getChildren().add(stackPane);
		// rootPane.setCenter(stackPane);
		rootPane.setPadding(new Insets(8, 8, 8, 8));

		stackPane.setClip(clip);
		stackPane.widthProperty().addListener((Observable observable) -> {
			clip.setWidth(stackPane.getWidth());
		});
		stackPane.heightProperty().addListener((Observable observable) -> {
			clip.setHeight(stackPane.getHeight());
		});

		backgroundColorPane.setStyle("-fx-background-color:rgba(245, 245, 245, 1)");

		stackPane.getChildren().add(backgroundColorPane);
		stackPane.getChildren().add(backgroundImagePane);
		stackPane.getChildren().add(centerPane);
		stackPane.getChildren().add(getTitlePane());

		// setRadius(3);
		// setDropShadowRadius(6);
		// setSpread(0.12);
	}

	private void initEvent() {
	}

	public void setCenter(Node value) {
		centerPane.getChildren().clear();
		centerPane.getChildren().add(value);
	}

	public void setBackgroundColor(Color color) {
		centerPane.setStyle("-fx-background-color:rgba(" + color.getRed() * 255 + "," + color.getGreen() * 255 + "," + color.getBlue() * 255 + ", " + color.getOpacity() + ")");
	}

	public void setRadius(double value) {
		clip.setArcHeight(value);
		clip.setArcWidth(value);
	}

	public void setDropShadowRadius(double value) {
		dropShadow.setRadius(value);
	}

	public void setSpread(double value) {
		dropShadow.setSpread(value);
	}

	public TitlePane getTitlePane() {
		return titlePane;
	}

	public StackPane getRootPane() {
		return rootPane;
	}

	public void setTitlePaneStyle(int style) {
		titlePane.setTitlePaneStyle(style);
	}
}
